<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title>登录</title>
		<!-- 样 式 文 件 -->
		<link rel="stylesheet" href="component/pear/css/pear.css" />
		<link rel="stylesheet" href="admin/css/other/login.css" />
	</head>
    <!-- 代 码 结 构 -->
	<body background="admin/images/background.svg" style="background-size: cover;">
		<form class="layui-form" action="javascript:void(0);" lay-filter="lms_form">
			<div class="layui-form-item">
				<img class="logo" src="admin/images/logo.png" />
				<div class="title">Pear Admin</div>
				<div class="desc">
					明 湖 区 最 具 影 响 力 的 设 计 规 范 之 一
				</div>
			</div>
			<div class="layui-form-item">
				<input name="lms_account" placeholder="账 户 : " required lay-verify="required" hover class="layui-input"  />
			</div>
			<div class="layui-form-item">
				<input name="lms_pass" type="password" autocomplete="off" required placeholder="密 码 : " lay-verify="required" hover class="layui-input"  />
			</div>
			<div class="layui-form-item">
				<input name="lms_captcha" placeholder="验证码 : "  hover  lay-verify="required" class="code layui-input"  />
				<img id="imgCaptcha" src="" class="codeImage" lay-filter="imgCaptcha"/>
			</div>
			<div class="layui-form-item">
				<!-- <input type="checkbox" name="" title="记住密码" lay-skin="primary" checked> -->
			</div>
			<div class="layui-form-item">
				<button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
					登 入
				</button>
			</div>
		</form>
		<!-- 资 源 引 入 -->
		<script src="component/layui/layui.js"></script>
		<script src="component/pear/pear.js"></script>
		<script src="lms/base64.js"></script>
		<!-- <script src="lms/md5.js"></script> -->
		<script src="lms/common.js"></script>
		<script src="lms/config.js"></script>
		<script>
			//鹰：
			if(checkLogin()) {
				location.href = lms_config.base_path + "index.html";
			}
			

			layui.use(['form', 'button', 'popup', 'jquery', 'http', 'encrypt'], function() {
				var form = layui.form;
				var button = layui.button;
				var popup = layui.popup;
				var $ = layui.jquery;
				var encrypt = layui.encrypt;

				const http = layui.http;
				
				
                // 登 录 提 交
				form.on('submit(login)', function(data) {
					
					/// 验证
					//layer.msg(JSON.stringify(data.field));
					//console.log(data);
					//console.log(form.val("lms_form"))
					
					/// 鹰：登录
					$.ajax({
						url: lms_config.api_url + "/adminapi/v1/Login/login", // 请求路径
						type: "POST", //请求方式
						//headers: {Authorization: getUserInfo()["token"]},
						contentType: 'application/x-www-form-urlencoded',
						//data: data.field, //请求参数
						data: {"account": data.field.lms_account,
							//"password": md5(data.field.lms_pass),
							"password": encrypt.md5(data.field.lms_pass),
							captcha: data.field.lms_captcha,
							cid: layui.sessionData("lms").captchaKey,
						},
						dataType: 'json',	//预期服务器返回的数据类型；设置接受到的响应数据的格式

						success: function (ret) {

							if(ret.code !== 0) {
								popup.failure(ret.msg, function() {
									refreshCaptcha();
									//location.href = lms_config.base_path + "index.html"
								});
								return;
							}
							
							//console.log(ret);
							//ret = JSON.parse(ret);
							//console.log(ret.data.token.split("."));
							//console.log(ret.data.token.split(".")[1]);
							//console.log(Base64.decode(ret.data.token.split(".")[1]));
							//console.log(Base64.decode('eyJleHAiOjE2NTYzODM5NDcsImlhdCI6MTY1NjI5NzU0NywiaXNzIjoibGVhbXVzLmNuIiwianRpIjoibHFnZXU0MDJybGxyb3JqMGFvb2xxMGdxcGM2MmI5MTg0YmNiZjkzMS44ODA0NjI4OSIsIm5iZiI6MTY1NjI5NzU0Nywic3ViIjoiTG1zX01hbmFnZW1lbnRQbGF0Zm9ybV92MS4wIiwicmVmcmVzaF90b2tlbl9leHBpcmUiOjE2NTY1NTY3NDcsImRhdGEiOnsibG9naW5uYW1lIjoicGxlYWZsZXMiLCJpZCI6MSwibmlja25hbWUiOiJcdTZkZjFcdTY3OTdcdTViNjRcdTllNzAiLCJzZXgiOjF9fQ'));

							// layui.data('lms', {
							// 	key: 'userInfo',
							// 	value: JSON.parse(Base64.decode(ret.data.token.split(".")[1]))
							// });
							setUserInfo({
								isLogin: true,
								"token": ret.data.token,
								info: ret.data.info,
								//user: JSON.parse(Base64.decode(ret.data.token.split(".")[1])),
								////user: JSON.parse(encrypt.Base64Decode(ret.data.token.split(".")[1])),
								rules: ret.data.rules,
								
							});
							
							/// 动画
							button.load({
								elem: '.login',
								time: 1500,
								done: function() {
									popup.success("登录成功", function() {
										location.href = lms_config.base_path + "index.html"
									});
								}
							});


						}, //响应成功后的回调函数
						error: function (ret) {
							console.error(ret);
							alert("出错" + ret.status + "：" + ret.responseText);
						}, //表示如果请求响应出现错误，会执行的回调函数

					});



					return false;
				});



				//鹰：点击验证码
				$("#imgCaptcha").click(function(){
					refreshCaptcha();
				});

				
				//鹰：刷新验证码
				refreshCaptcha();
				function refreshCaptcha() {
					$.ajax({
						url: lms_config.api_url + "/adminapi/v1/Login/getCaptcha", //请求路径
						type: "GET", //请求方式
						//headers: {Authorization: getUserInfo()["token"]},
						contentType: 'application/x-www-form-urlencoded',
						data: "", //请求参数
						//data:{"username":"jack","age":23},
						dataType: 'json',	//预期服务器返回的数据类型；设置接受到的响应数据的格式

						success: function (data) {
							//console.log(data);
							//data = JSON.parse(data);
							//console.log(layui.sessionData("captchaKey"), layui.data("captchaKey"))

							$("#imgCaptcha").attr("src", 'data:image/png;base64,' + data.data.img);
							layui.sessionData("lms", {
								key: "captchaKey",
								value: data.data.key
							});

							//console.log(layui.sessionData("lms"), layui.data("lms"))
							//console.log($("#imgCaptcha"))
							//console.log($("#imgCaptcha").attr("src"))
							////console.log($("#imgCaptcha").Val());

						}, //响应成功后的回调函数
						error: function (data) {
							console.log(data);
							//alert("出错啦...")
						}, //表示如果请求响应出现错误，会执行的回调函数

					});
				}
			})
		</script>
	</body>
</html>